package com.sjl.hutool;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.lang.Singleton;
import cn.hutool.core.thread.ThreadFactoryBuilder;
import cn.hutool.core.thread.ThreadUtil;
import cn.hutool.log.Log;
import cn.hutool.log.StaticLog;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.text.translate.UnicodeEscaper;
import org.apache.commons.text.StringEscapeUtils;

import java.util.concurrent.*;

/**
 * @author sjl
 * @date 2019年03月20日10:29:29
 */
public class hutoolTest {

    public static void main(String[] args) {

        long start = System.currentTimeMillis();

        ExecutorService singleThreadPool =  ThreadUtil.newExecutor();
        
        singleThreadPool.execute(()-> System.out.println(Thread.currentThread().getName()));
        singleThreadPool.execute(()-> System.out.println(Thread.currentThread().getName()));
        singleThreadPool.execute(()-> System.out.println(Thread.currentThread().getName()));
        singleThreadPool.submit(()->System.out.println(Thread.currentThread().getName()));
        singleThreadPool.shutdown();

//        ThreadUtil.execute(()-> System.out.println("默认线程池:"+Thread.currentThread().getName()));
        Future<Integer> integerFuture = ThreadUtil.execAsync(() -> 1 + 2);
        try {
            Integer integer = integerFuture.get();

            System.out.println(integer);
        }catch (Exception e){
            StaticLog.error(e);
        }
        ExecutorService singleThreadPool2 =ThreadUtil.newSingleExecutor();
        singleThreadPool2.execute(()->System.out.println("新建单例线程池"+Thread.currentThread().getName()));
        singleThreadPool2.shutdown();

//        System.out.println(singleThreadPool2.isShutdown());

        Thread[] threads = ThreadUtil.getThreads();
        for (Thread t :threads) {
            System.out.println(t.getName());
        }
        System.err.println(System.currentTimeMillis()-start);
        System.err.println(DateUtil.date().toString());


        ScheduledExecutorService scheduledExecutorService = Executors.newScheduledThreadPool(1);
        Future<String> submit = scheduledExecutorService.submit(() -> tst());
        String result  = null;
        try {
            result = submit.get();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }
        scheduledExecutorService.shutdown();
        System.out.println(result);
    }


    public   static String tst(){
        return "ss";
    }
}
